package com.tiancheng.trade.order.controller.app;

import com.tiancheng.trade.commom.core.data.PageInfo;
import com.tiancheng.trade.commom.core.data.Result;
import com.tiancheng.trade.order.util.SecurityUtils;
import com.tiancheng.trade.order.configuration.RequestForm;
import com.tiancheng.trade.order.service.IntOrderService;
import com.tiancheng.trade.order.service.OrderService;
import com.tiancheng.trade.order.vo.app.*;
import com.tiancheng.trade.order.vo.app.res.*;
import com.tiancheng.trade.order.vo.open.order.OpenCashierResVO;
import com.tiancheng.trade.order.vo.open.order.OpenOrderPayVO;
import com.tiancheng.trade.order.vo.parallel.AppleInAppPayVO;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 订单移动端接口
 *
 * @author kellen
 * @date 2024/5/11
 */
@RestController
@RequestMapping("/app")
@Validated
@Slf4j
public class OrderAppController {

    @Resource
    private OrderService orderService;
    @Resource
    private IntOrderService intOrderService;

    /**
     * 初始化收银台
     *
     * @param cashierVO
     * @return
     */
    @PostMapping(value = "/cashier", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result cashier(@RequestBody @Valid CashierVO cashierVO, @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();

        if (headers.containsKey("apptype")) {
            cashierVO.setAppType(headers.getValuesAsList("apptype").get(0));
        }
        if (headers.containsKey("appversion")) {
            cashierVO.setAppVersion(headers.getValuesAsList("appversion").get(0));
        }

        final OpenCashierResVO openCashierResVO = orderService.initCashier(uid, cashierVO);
        return Result.success(openCashierResVO);
    }

    /**
     * 初始化收银台
     *
     * @param cashierVO
     * @return
     */
    @PostMapping(value = "/cashier", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result cashierForm(@RequestForm @Valid CashierVO cashierVO, @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();

        if (headers.containsKey("apptype")) {
            cashierVO.setAppType(headers.getValuesAsList("apptype").get(0));
        }
        if (headers.containsKey("appversion")) {
            cashierVO.setAppVersion(headers.getValuesAsList("appversion").get(0));
        }

        final OpenCashierResVO openCashierResVO = orderService.initCashier(uid, cashierVO);
        return Result.success(openCashierResVO);
    }

    /**
     * 支付订单
     *
     * @param orderPayVO
     * @return
     */
    @PostMapping(value = "/pay", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result pay(@RequestBody @Valid OpenOrderPayVO orderPayVO, @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();
        if (StringUtils.isEmpty(orderPayVO.getOpenid()) && headers.containsKey("openid")) {
            orderPayVO.setOpenid(headers.getValuesAsList("openid").get(0));
        }

        OrderPrePayResVO orderPrePayResVO = orderService.pay(uid, orderPayVO);
        return Result.success(orderPrePayResVO);
    }

    /**
     * 支付订单
     *
     * @param orderPayVO
     * @return
     */
    @PostMapping(value = "/pay", consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
    public Result payForm(@RequestForm @Valid OpenOrderPayVO orderPayVO, @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();
        if (StringUtils.isEmpty(orderPayVO.getOpenid()) && headers.containsKey("openid")) {
            orderPayVO.setOpenid(headers.getValuesAsList("openid").get(0));
        }
        OrderPrePayResVO orderPrePayResVO = orderService.pay(uid, orderPayVO);
        return Result.success(orderPrePayResVO);
    }

    /**
     * 查询用户订单
     *
     * @param orderQueryVO
     * @return
     */
    @GetMapping(value = "/query")
    public Result query(OrderQueryVO orderQueryVO, @RequestHeader HttpHeaders headers) {
        if (orderQueryVO.getSize() == 0) {
            orderQueryVO.setSize(10);
        }
        if (headers.containsKey("apptype")) {
            orderQueryVO.setOrderScene(headers.getValuesAsList("apptype").get(0));
        }
        PageInfo<OrderQueryResVO> pageInfo = orderService.findForApp(orderQueryVO);
        return Result.success(pageInfo);
    }

    /**
     * 获取订单的支付状态
     *
     * @param orderId
     * @return
     */
    @GetMapping("/getPayStatus")
    public Result getPayStatus(@RequestParam("order_id") String orderId) {
        String uid = SecurityUtils.getLoginUserId();
        Map<String, String> res = new HashMap<>();
        res.put("pay_status", orderService.getPayStatus(uid, orderId));
        return Result.success(res);
    }

    /**
     * 个人中心展示服务中订单查询
     *
     * @param queryVO {@link OrderCenterQueryVO}
     * @return {@link Result}
     */
    @GetMapping("/getCenterOrder")
    public Result<?> getCenterOrder(OrderCenterQueryVO queryVO) {
        PageInfo<OrderQueryResVO> pageInfo = orderService.getCenterOrder(queryVO);
        return Result.success(pageInfo);
    }


    /**
     * paypal支付订单创建
     *
     * @param paypalCreateVO
     * @return
     */
    @PostMapping(value = "/paypalCreate", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result paypalCreate(@RequestBody @Valid PaypalCreateReqVO paypalCreateVO, @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();
        if (StringUtils.isEmpty(paypalCreateVO.getOpenid()) && headers.containsKey("openid")) {
            paypalCreateVO.setOpenid(headers.getValuesAsList("openid").get(0));
        }
        if (headers.containsKey("apptype")) {
            paypalCreateVO.setAppType(headers.getValuesAsList("apptype").get(0));
        }
        if (headers.containsKey("appversion")) {
            paypalCreateVO.setAppVersion(headers.getValuesAsList("appversion").get(0));
        }
        PaypalCreateResVO paypalCreateResVO = intOrderService.createPaypal(uid, paypalCreateVO);
        return Result.success(paypalCreateResVO);
    }

    /**
     * paypal支付订单确认
     *
     * @param paypalExecuteReqVO
     * @return
     */
    @PostMapping(value = "/paypalExecute", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result paypalExecute(@RequestBody @Valid PaypalExecuteReqVO paypalExecuteReqVO, @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();
        log.info("确认支付获取到的uid:{}", uid);
        PaypalExecuteResVO paypalExecuteResVO = intOrderService.executePaypay(uid, paypalExecuteReqVO);
        return Result.success(paypalExecuteResVO);
    }


    /**
     * 英文版APP查询用户订单列表
     *
     * @param orderQueryVO
     * @return
     */
    @GetMapping(value = "/intQuery")
    public Result intQuery(OrderQueryVO orderQueryVO, @RequestHeader HttpHeaders headers) {
        if (orderQueryVO.getSize() == 0) {
            orderQueryVO.setSize(10);
        }
        if (headers.containsKey("apptype")) {
            orderQueryVO.setOrderScene(headers.getValuesAsList("apptype").get(0));
        }
        PageInfo<IntOrderQueryResVO> pageInfo = intOrderService.findForApp(orderQueryVO);
        return Result.success(pageInfo);
    }

    /**
     * 英文版APP初始化收银台信息查询
     *
     * @param intCashierVO
     * @return
     */
    @PostMapping(value = "/intCashier", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result internationCashier(@RequestBody @Valid IntCashierReqVO intCashierVO,
                                     @RequestHeader HttpHeaders headers) {
        String uid = SecurityUtils.getLoginUserId();

        if (headers.containsKey("apptype")) {
            intCashierVO.setAppType(headers.getValuesAsList("apptype").get(0));
        }
        if (headers.containsKey("appversion")) {
            intCashierVO.setAppVersion(headers.getValuesAsList("appversion").get(0));
        }

        IntCashierResVO intCashierResVO = intOrderService.initCashier(uid, intCashierVO);
        return Result.success(intCashierResVO);
    }

    /**
     * 英文版APP获取订单的支付状态
     *
     * @param orderId
     * @return
     */
    @GetMapping("/getIntPayStatus")
    public Result getIntPayStatus(@RequestParam("order_id") String orderId) {
        String uid = SecurityUtils.getLoginUserId();
        log.info("APP获取订单的支付状态的uid:{}", uid);
        Map<String, String> res = new HashMap<>();
        res.put("pay_status", intOrderService.getPayStatus(uid, orderId));
        return Result.success(res);
    }


    /**
     * 支付
     * @param appleInAppPayVO
     * @return
     */
    @PostMapping(value = "/appleInAppPay", consumes = MediaType.APPLICATION_JSON_VALUE)
    public Result appleInAppPay(@RequestBody @Valid AppleInAppPayVO appleInAppPayVO) {
        String uid = SecurityUtils.getLoginUserId();
        if(StringUtils.isEmpty(appleInAppPayVO.getUid())){
            appleInAppPayVO.setUid(uid);
        }
        return Result.success(orderService.appleInAppPay(appleInAppPayVO));
    }


}
